package cn.txauction.web.struts.action.base;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;

import cn.txauction.business.service.IAdminService;
import cn.txauction.business.service.ICommonService;
import cn.txauction.common.exception.TxPermissionDeniedException;

/**
 * FIXME 防止表单重复提交(后退后再次提交)
 * @author 
 */
public class BaseAction extends DispatchAction {
	protected static Log log = LogFactory.getLog(BaseAction.class);

	@Resource protected IAdminService adminService;
	@Resource protected ICommonService commonService;


	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			return super.execute(mapping, form, request, response);
		}
		// 如果没有权限执行某个方法
		catch (TxPermissionDeniedException e) {
			// 1. 如果未登陆, 转到登陆界面
//			if (!WebCommonUtil.isLoggedOn(request)) {
//				// 可以在登陆后再转到登陆前的页面, 这个页面地址可以放到 request 作用域中;
//				// 或是不放, 这将使用出现异常时访问的页面
//				String returnPath = (String) request.getAttribute("returnPath");
//				WebCommonUtil.toLogin(request, response, returnPath);
//				return null;
//			}

			// 2. 如果已登陆, 抛出权限不足异常
			throw e;
		}
	}

	/**
	 * @param request
	 * @return 当前存在session作用域(登陆)的用户
	 */
//	protected User getCurrentUser(HttpServletRequest request) {
//		return WebCommonUtil.getLoggedOnUser(request);
//	}
//
//	/**
//	 * 更新application作用域中的categories集合
//	 * 
//	 * @param request
//	 */
//	protected void setCategoriesInRequestScope(HttpServletRequest request) {
//		List<Category> categories = categoryService.findAll();
//		request.setAttribute(WebConstants.SCOPE_ATTR_CATEGORIES, categories);
//	}

	/**
	 * 验证FormBean, 通过调用 {@link ActionForm#validate(ActionMapping, HttpServletRequest)}方法
	 * 
	 * @param form
	 * @param mapping
	 * @param request
	 * @return 表单是否验证失败
	 */
	protected boolean validateFailed(ActionForm form, ActionMapping mapping, HttpServletRequest request) {
		ActionMessages errors = form.validate(mapping, request);
		if (errors != null && errors.size() > 0) {
			this.saveErrors(request, errors);
			return true;
		}
		return false;
	}



	/**
	 * 获取布尔型参数值
	 * 
	 * @param request
	 * @param name
	 * @return
	 */
	protected boolean getBoolParam(HttpServletRequest request, String name) {
		return "true".equals(request.getParameter(name));
	}

	/**
	 * 获取int型参数值. 如果没有传递, 则返回 defaultValue
	 * 
	 * @param request
	 * @param name
	 * @param defaultValue
	 *            默认值
	 * @return
	 */
	protected int getIntParam(HttpServletRequest request, String name, int defaultValue) {
		try {
			return Integer.parseInt(request.getParameter(name));
		} catch (NumberFormatException e) {
			return defaultValue;
		}
	}
}
